package com.zufangbao.earth.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.zufangbao.earth.entity.particalsystem.ParticalModel;
import com.zufangbao.earth.service.ParticalModelService;

@Service("particalModelService")
public class ParticalModelServiceImpl implements ParticalModelService {

	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Override
	public String getValue(String particalUniqueId, String key) {
		
		ParticalModel particalModel = getParticalModel(particalUniqueId, key);
		
		if(null == particalModel){
			
			return null;
		}
		return particalModel.getValue();
				
	}

	@Override
	public void setValue(String particalUniqueId, String key, String value) {
		
		boolean isExist = getValue(particalUniqueId, key) != null;
		
		if(isExist){
			
			return;
		}
		
		ParticalModel particalModel = new ParticalModel(particalUniqueId,key,value);
		
		genericDaoSupport.save(particalModel);
	}

	@Override
	public void delValue(String particalUniqueId, String key) {
		
		String querySentence = "FROM ParticalModel particalModel WHERE particalModel.uniqueParticalId = :uniqueParticalId AND particalModel.name = :name";
		
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		
		paramsMap.put("uniqueParticalId",particalUniqueId);
		
		paramsMap.put("name", key);
		
		@SuppressWarnings("unchecked")
		List<ParticalModel> particalModelList = genericDaoSupport.searchForList(querySentence, paramsMap);
		
		
		if(CollectionUtils.isEmpty(particalModelList)){
			
			return;
		}
		
		genericDaoSupport.delete(particalModelList.get(0));

	}
	@Override
	public List<ParticalModel> loadAllParticalModels(String particalUniqueId) {
		
		if(StringUtils.isEmpty(particalUniqueId)){
			
			return null;
		}
		
		String querySentence = "FROM ParticalModel particalModel WHERE particalModel.uniqueParticalId = :uniqueParticalId";
		
		@SuppressWarnings("unchecked")
		List<ParticalModel> particalModelList = genericDaoSupport.searchForList(querySentence, "uniqueParticalId", particalUniqueId);
		
		return particalModelList;
		
	}

	@Override
	public void updateFromParticalModels(List<ParticalModel> particalModelList) {
		
		if(CollectionUtils.isEmpty(particalModelList)){
			
			return;
		}
		for(ParticalModel particalModel : particalModelList){
			
			updateFromParticalModel(particalModel);
		}
		
	}
	private void updateFromParticalModel(ParticalModel particalModel){
		
		genericDaoSupport.saveOrUpdate(particalModel);
	}

	@Override
	public ParticalModel getParticalModel(String particalUniqueId, String key) {
		
		String querySentence = "FROM ParticalModel particalModel WHERE particalModel.uniqueParticalId = :uniqueParticalId AND particalModel.name = :name";
		
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		
		paramsMap.put("uniqueParticalId",particalUniqueId);
		
		paramsMap.put("name", key);
		
		@SuppressWarnings("unchecked")
		List<ParticalModel> particalModelList = genericDaoSupport.searchForList(querySentence, paramsMap);
		
		if(CollectionUtils.isEmpty(particalModelList)){
			
			return null;
		}
		
		return particalModelList.get(0);
	}

	@Override
	public void delValueFromIds(List<Long> particalModelIdList) {

		for(Long particalModelId : particalModelIdList){
			
			genericDaoSupport.delete(ParticalModel.class, particalModelId);
		}
	}

	@Override
	public void delValueByParticalUniqueId(String particalUniqueId) {
		
		if(StringUtils.isEmpty(particalUniqueId)){
			return;
		}
		List<ParticalModel> particalModelList = loadAllParticalModels(particalUniqueId);
		
		if(CollectionUtils.isEmpty(particalModelList)){
			
			return;
		}
		
		for(ParticalModel particalModel : particalModelList){
			
			genericDaoSupport.delete(particalModel);
		}
		
	}

	@Override
	public void updateParticalModelWithUniqueId(String originUniqueId,
			String newUniqueId) {
		
		if(StringUtils.isEmpty(originUniqueId) || StringUtils.isEmpty(newUniqueId)){
			
			return;
		}
		
		List<ParticalModel> particalModels = loadAllParticalModels(originUniqueId);
		
		if(CollectionUtils.isEmpty(particalModels)){
			
			return;
		}
		
		for(ParticalModel particalModel : particalModels){
			
			particalModel.setUniqueParticalId(newUniqueId);
			
			genericDaoSupport.saveOrUpdate(particalModel);
		}
		
	}

}
